package com.adminsystem.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.adminsystem.entity.ExamPaperQuestion;
import com.adminsystem.service.IExamPaperQuestionService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 考试题目Controller
 *
 * @author admin
 * @date 2023-11-23
 */
@RestController
@RequestMapping("/examManage/question")
public class ExamPaperQuestionController extends BaseController {
    @Autowired
    private IExamPaperQuestionService examPaperQuestionService;

    /**
     * 查询考试题目列表
     */
    @PreAuthorize("hasAuthority('examManage:question:userList')|| hasAuthority('examManage:question:list')")
    @GetMapping("/list")
    public TableDataInfo list(ExamPaperQuestion examPaperQuestion) {
        startPage();
        List<ExamPaperQuestion> list = examPaperQuestionService.selectExamPaperQuestionList(examPaperQuestion);
        return getDataTable(list);
    }

    /**
     * 导出考试题目列表
     */
    @PreAuthorize("hasAuthority('examManage:question:export')")
    @Log(title = "考试题目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ExamPaperQuestion examPaperQuestion) {
        List<ExamPaperQuestion> list = examPaperQuestionService.selectExamPaperQuestionList(examPaperQuestion);
        ExcelUtil<ExamPaperQuestion> util = new ExcelUtil<ExamPaperQuestion>(ExamPaperQuestion.class);
        util.exportExcel(response, list, "考试题目数据");
    }

    /**
     * 获取考试题目详细信息
     */
    @PreAuthorize("hasAuthority('examManage:question:query')")
    @GetMapping(value = "/{questionId}")
    public AjaxResult getInfo(@PathVariable("questionId") Long questionId) {
        return success(examPaperQuestionService.selectExamPaperQuestionByQuestionId(questionId));
    }

    /**
     * 新增考试题目
     */
    @PreAuthorize("hasAuthority('examManage:question:add')")
    @Log(title = "考试题目", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ExamPaperQuestion examPaperQuestion) {
        return toAjax(examPaperQuestionService.insertExamPaperQuestion(examPaperQuestion));
    }

    /**
     * 修改考试题目
     */
    @PreAuthorize("hasAuthority('examManage:question:edit')")
    @Log(title = "考试题目", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ExamPaperQuestion examPaperQuestion) {
        return toAjax(examPaperQuestionService.updateExamPaperQuestion(examPaperQuestion));
    }

    /**
     * 删除考试题目
     */
    @PreAuthorize("hasAuthority('examManage:question:remove')")
    @Log(title = "考试题目", businessType = BusinessType.DELETE)
    @DeleteMapping("/{questionIds}")
    public AjaxResult remove(@PathVariable Long[] questionIds) {
        return toAjax(examPaperQuestionService.deleteExamPaperQuestionByQuestionIds(questionIds));
    }

    /**
     * 根据试卷ID查询考试题目列表
     */
    @PreAuthorize("hasAuthority('examManage:question:userList')|| hasAuthority('examManage:question:list')")
    @GetMapping("/listByPaperId")
    public TableDataInfo listByPaperId(ExamPaperQuestion examPaperQuestion) {
        startPage();
        List<ExamPaperQuestion> list = examPaperQuestionService.selectExamPaperQuestionListByPaperId(examPaperQuestion);
        return getDataTable(list);
    }

    /**
     * 根据试卷ID查询考试题目列表（未选择的考题）
     */
    @PreAuthorize("hasAuthority('examManage:question:userList')|| hasAuthority('examManage:question:list')")
    @GetMapping("/listByExPaperId")
    public TableDataInfo listByExPaperId(ExamPaperQuestion examPaperQuestion) {
        startPage();
        List<ExamPaperQuestion> list = examPaperQuestionService.selectExamPaperQuestionListExByPaperId(examPaperQuestion);
        return getDataTable(list);
    }

    /**
     * 根据试卷ID查询考试题目列表(详细)
     */
    @PreAuthorize("hasAuthority('examManage:question:userList')|| hasAuthority('examManage:question:list')")
    @GetMapping("/listAllByPaperId")
    public TableDataInfo listAllByExPaperId(ExamPaperQuestion examPaperQuestion) {
        startPage();
        List<ExamPaperQuestion> list = examPaperQuestionService.selectExamPaperQuestionAllList(examPaperQuestion);
        return getDataTable(list);
    }
}
